/**
 * 同一个js文件被导入多次，他们引用的还是同一个内存对象
 * 例如Vue模块再其它文件被导入，他和此文件的Vue引用同一个对象
 */
import Vue from "vue";
import store from "./vuex/store";
import VueRouter from "vue-router";
import routes from "./routers"; // 引入路由配置
import VueLazyload from "vue-lazyload"; // 引入图片懒加载模块
import "common/css/index.styl"; // 引入公共样式
import common from "./common/js/common.js";
import sharejs from "./common/js/sharejs.js";
import $ from "jquery";
import Carousel3d from "vue-carousel-3d";
import "../static/js/rem.js";
import axios from "./common/js/http.js";

// 注册组件
Vue.use(Carousel3d);
Vue.use(common);
Vue.use(VueRouter);

// error，loading是图片路径, 用require引入 attempt为加载图片的次数
Vue.use(VueLazyload, {
  error: require("./assets/404.png"),
  loading: require("./assets/loading.gif"),
  attempt: 1,
  throttleWait: 300
});

const router = new VueRouter({
  mode: "history",
  linkActiveClass: "active", // 路由当前激活的状态
  base: "/games/dianwu", //线上配置
  routes, // （缩写）相当于 routes: routes
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      //回退的时候才会触发
      return savedPosition;
    } else {
      if (to.meta.keepAlive) return { x: 0, y: to.meta.savedPosition || 0 };
      else return { x: 0, y: 0 };
    }
  }
});
axios.globalRouter = router;
Vue.prototype.$http = axios;

//防止安卓界面弹出软键盘 界面被顶起
$(document).ready(function() {
  $("body").height($("body")[0].clientHeight);
});
//获取cookie内的openid
let userid = window.getCookie("aljx_userid");
let storeid = window.getCookie("aljx_storeid");
let storeName = window.getCookie("aljx_sname");
let shareImg = store.state.soursePath + window.getCookie("aljx_icon");

if (process.env.NODE_ENV != "production") {
  store.commit("UPDATE_USERID", "457");
  store.commit("UPDATE_STOREID", "18");
} else {
  store.commit("UPDATE_USERID", userid);
  store.commit("UPDATE_STOREID", storeid);
}
// 路由跳转之前回掉函数
router.beforeEach((to, from, next) => {
  if (from.meta.keepAlive) {
    from.meta.savedPosition = document.body.scrollTop;
  }
  store.dispatch("toggleLoading", true);
  if (!store.state.iosPath) {            //是否是入口页面
    let landingPage = document.URL;
    if (to.name == "home" && landingPage.indexOf("home") < 0) {
      landingPage = landingPage + "home";
    }
    store.commit("UPDATA_IOS_PATH", landingPage);
    window.history.replaceState({}, document.title, landingPage);
    Vue.prototype.getInitData(() => {
      next();
      if(store.state.giftList&&store.state.giftList.length>0&&to.path!='/bindPhone'){
        store.commit("UPDATA_SHOW_GIFT_POPUP", true);
      }
      if(store.state.sistem=='ios'){
        sharejs.defultShareMsg();
        sharejs.getWxData();
      }
    });
  } else next();
});
router.afterEach(to => {
  store.dispatch("toggleLoading", false);
  if (to.meta.title) {
    if (to.meta.title == " " && store.state.storeInfo.name)
      window.setDocumentTitle(store.state.storeInfo.name);
    else window.setDocumentTitle(to.meta.title);
  }

  if (to.name != "details" && store.state.sistem != "ios") {
    let shareClearId = setTimeout(() => {
      sharejs.defultShareMsg();
      sharejs.getWxData();
    }, 300);
  }
});

/**
 * 创建和挂载根实例。
 * 记得要通过 router 配置参数注入路由，
 * 从而让整个应用都有路由功能
 */
const routerApp = new Vue({
  store,
  router
});

document.addEventListener("DOMContentLoaded", function() {
  routerApp.$mount("#app");
});

export default routerApp;
